package linkedlist

import "fmt"

/**
 *Definition for singly-linked list.
 */
type ListNode struct {
	Val  int
	Next *ListNode
}

func Print(head *ListNode) {
	if head == nil {
		fmt.Println("NULL")
		return
	}
	fmt.Print(head.Val, "->")
	Print(head.Next)
}

func BuildLinkedList(nums ...int) *ListNode {
	if len(nums) == 0 {
		return nil
	}
	head := &ListNode{nums[0], nil}
	var prev = head
	for i := 1; i < len(nums); i++ {
		prev.Next = &ListNode{nums[i], nil}
		prev = prev.Next
	}
	return head
}

func Slice2LinkedList(nums []int) *ListNode {
	if len(nums) == 0 {
		return nil
	}
	head := &ListNode{nums[0], nil}
	var prev = head
	for i := 1; i < len(nums); i++ {
		prev.Next = &ListNode{nums[i], nil}
		prev = prev.Next
	}
	return head
}

func LinkedList2Slice(head *ListNode) []int {
	nums := make([]int, 0)
	if head == nil {
		return nums
	}
	for cur := head; cur != nil; cur = cur.Next {
		nums = append(nums, cur.Val)
	}
	return nums
}
